package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.query.DepartmentQuery;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@RestController=@ResponseBody+@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    /***
     * 添加和修改
     * @param department
     * @return
     */
    @PutMapping// @PutMapping = @RequestMapping(method = RequestMethod.PUT)
    public AjaxResult add(@RequestBody Department department){
        try {
            service.addorupdate(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult dele(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param departments
     * @return
     */
    @PatchMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            service.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 修改
     * @param department
     * @return
     */
    @PostMapping
    public AjaxResult update(@RequestBody Department department){
        try {
            service.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 单个查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Department findOne(@PathVariable("id") Long id){

            return  service.findOne(id);
    }

    /**
     * 全部查询
     * 分页
     * 高级查询
     * @param departmentQuery
     * @return
     */
    @PatchMapping
    public PageList<Department> findAll(@RequestBody DepartmentQuery departmentQuery) {

        return service.findAll(departmentQuery);
    }

    /**
     * 查询部门列表
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Department> loadAll() {
        return service.loadAll();
    }
}
